package com.example.yoho.mvp.presenter;

import android.util.Log;

import com.example.lib_core.base.BaseEntity;
import com.example.lib_core.mvp.presenter.BasePresenter;
import com.example.yoho.mvp.contract.HomeContract;
import com.example.yoho.mvp.model.entity.BannerEntity;
import com.example.yoho.mvp.model.entity.HomeGoodsEntity;
import com.example.yoho.mvp.model.entity.RecommendEntity;
import com.example.yoho.mvp.model.entity.UpgradeEntity;

import org.json.JSONException;
import org.json.JSONObject;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class HomePresenter extends BasePresenter<HomeContract.Model,HomeContract.View> {


    private int page=0;
    private int menuId=1;
    private int categoryId=1;
    private int version_code=2;
    @Inject
    public HomePresenter() {
    }

    public void HomeRequestAll(int menuId,int categoryId){
        this.menuId=menuId;
        this.categoryId=categoryId;
        this.version_code=version_code;
        page=0;
        JSONObject recommendJob=new JSONObject();
        JSONObject goodsJob=new JSONObject();
        JSONObject upgradeJob = new JSONObject();

        try {
            recommendJob.put("menu","1");
            goodsJob.put("category","1");
            goodsJob.put("page",""+page);
            upgradeJob.put("version_code","2");
        } catch (JSONException e) {
            e.printStackTrace();
        }
        Observable<BaseEntity>ob = Observable.merge(mModel. bannerModel(),
                mModel.homegoodsModel(goodsJob.toString()),
                mModel.recommendModel(recommendJob.toString()),mModel.upgradeModel(upgradeJob.toString()));
        ob.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(new Observer<BaseEntity>() {
            @Override
            public void onSubscribe(Disposable d) {
                disposables.add(d);
            }

            @Override
            public void onNext(BaseEntity baseEntity) {
                if (baseEntity instanceof BannerEntity){
                    BannerEntity bannerEntity= (BannerEntity) baseEntity;
                    mView.showBanner(bannerEntity.values);
                }else if (baseEntity instanceof RecommendEntity){
                    RecommendEntity recommendEntity= (RecommendEntity) baseEntity;
                    mView.showRecommend(recommendEntity);
                }else if (baseEntity instanceof HomeGoodsEntity){
                    HomeGoodsEntity entity= (HomeGoodsEntity) baseEntity;
                    mView.showGoods(entity.values);
                }else if (baseEntity instanceof UpgradeEntity){
                    UpgradeEntity entity= (UpgradeEntity) baseEntity;
                    mView.showUpgrade(entity);
                }
            }

            @Override
            public void onError(Throwable e) {
                mView.showToast("请求出错"+e.getMessage());
            }

            @Override
            public void onComplete() {
                mView.hideDialog();
            }
        });
    }
    //上拉加载进入
    public void HomeRequstLoad(){
        ++page;
        JSONObject goodsJob = new JSONObject();
        try {
            goodsJob.put("category",""+categoryId);
            goodsJob.put("page",""+page);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        mModel.homegoodsModel(""+goodsJob.toString()).subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<HomeGoodsEntity>() {
                    @Override
                    public void onSubscribe(Disposable d) {
                        disposables.add(d);
                        mView.showDialog();
                    }

                    @Override
                    public void onNext(HomeGoodsEntity homeGoodsEntity) {
                       if (homeGoodsEntity!=null&&homeGoodsEntity.values!=null){

                               Log.i("ysk", "onNext:没有更多了.....");
                               mView.showLoadGoods(homeGoodsEntity.values);



                       }else{
                           Log.i("ysk", "onNext:没有更多了..... ");

                           mView.showToast("没有更多了.....");
                       }
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.hideDialog();
                        mView.showToast("请求出错:"+e.getMessage());
                    }

                    @Override
                    public void onComplete() {
                        mView.hideDialog();
                    }
                });
    }



}
